package com.gyt.manager.controller.risk;

import com.gyt.manager.common.base.BaseController;
import com.gyt.manager.common.base.response.Page;
import com.gyt.manager.common.base.response.Response;
import com.gyt.manager.domain.req.LabelExcelReq;
import com.gyt.manager.domain.req.LabelLevelReq;
import com.gyt.manager.domain.req.LabelReq;
import com.gyt.manager.domain.risk.LabelInfo;
import com.gyt.manager.service.LabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 标签.
 *
 * @author gyt
 */
@RestController
@RequestMapping("resource")
@Api(tags = "resource-label", description = "资源中心-标签", value = "label")
public class LabelController extends BaseController {

    @Autowired
    private LabelService labelService;

    @PostMapping("label/sendEmail.do")
    @ApiOperation(value = "发送标签日志告警信息", notes = "标签")
    public Response<?> sendEmail(
            @ApiParam(value = "日期", required = true)
            @RequestParam(value = "fdate") final String fdate) {
        try {
            labelService.sendEmail(fdate);
            return Response.ok(null);
        } catch (Exception e) {
            return Response.error(null);
        }
    }

    @PostMapping("label/batchAddLabelAlarmInfo.do")
    @ApiOperation(value = "发送标签等级不匹配告警", notes = "标签")
    public Response<?> batchAddLabelAlarmInfo(
            @ApiParam(value = "日期", required = true)
            @RequestParam(value = "fdate") final String fdate) {
        labelService.batchAddLabelAlarmInfo(fdate);
        return Response.ok(null);
    }

    @PostMapping("label/batchAddLabelInfo.do")
    @ApiOperation(value = "批量更新标签信息", notes = "标签")
    public Response<?> batchAddLabelInfo(
            @ApiParam(value = "日期", required = true)
            @RequestParam(value = "fdate") final String fdate) {
        labelService.batchAddLabelInfo(fdate);
        return Response.ok(null);
    }

    @PostMapping("label/findLabelInfos.do")
    @ApiOperation(value = "获取标签列表信息", notes = "标签")
    public Response<Page<LabelInfo>> findLabelInfos(
            @ApiParam(value = "请求对象")
            @RequestBody LabelReq labelReq) {
        String userName = "yechunwei";
        Page<LabelInfo> results =
                labelService.findLabelInfos(userName, labelReq);
        return Response.ok(results);
    }

    @PostMapping("label/batchUpdateLabelLevel.do")
    @ApiOperation(value = "批量更新标签等级信息", notes = "标签")
    public Response<Boolean> batchUpdateLabelLevel(
            @ApiParam(value = "白名单请求对象")
            @Valid @RequestBody final LabelLevelReq labelLevelReq) {
        Boolean flag = labelService.batchUpdateLabelLevel(labelLevelReq);
        return Response.ok(flag);
    }

    @PostMapping("label/whetherLockedLabel.do")
    @ApiOperation(value = "是否锁定标签", notes = "标签")
    public Response<Boolean> whetherLockedLabel(
            @ApiParam(value = "主键id", required = true)
            @RequestParam(value = "fid") final Long fid,
            @ApiParam(value = "是否锁定:unlock-不锁定；lock-锁定", required = true)
            @RequestParam(value = "isLock") final String isLock) {
        Boolean flag = labelService.whetherLockedLabel(fid, isLock);
        return Response.ok(flag);
    }

    @GetMapping("label/findLabelStatus.do")
    @ApiOperation(value = "获取所有标签状态", notes = "标签")
    public Response<List<Map<String, Object>>> findLabelStatus() {
        List<Map<String, Object>> results =
                labelService.findLabelStatus();
        return Response.ok(results);
    }

    @GetMapping("label/exportLabelToExcel.do")
    @ApiOperation(value = "导出标签数据(export)", notes = "测试")
    public Response exportLabelToExcel() {
        try {
            LabelExcelReq req = new LabelExcelReq();
            byte[] bytes = labelService.exportLabelToExcel(req, response);
            return Response.ok(bytes);
        } catch (Exception e) {
            return Response.error(null);
        }
    }


}
